var api = require("api");
var util=require("../utils/util");


// 拼团课程列表
function get_group_list(params) {
  return new Promise((resolve,reject)=>{
    wx.request({
      url: api.data.requestUrl + api.data.getGroupList,
      header: {
        'content-type': 'application/json;charset=UTF-8'
      },
      method: 'GET',
      data: params,
      success: function (res) {
        resolve(res);
        util.show_success_toast(res)
      },
      fail: function () {
        reject();
        util.show_fail_toast()
      },
    })
  })
}
// 拼团课程详情
function get_cour_and_group(params) {
  return new Promise((resolve,reject)=>{
    wx.request({
      url: api.data.requestUrl + api.data.getCourAndGroup,
      header: {
        'content-type': 'application/json;charset=UTF-8',
        'x-access-token': wx.getStorageSync('token')?wx.getStorageSync('token'):''
      },
      method: 'GET',
      data: params,
      success: function (res) {
        resolve(res);
        util.show_success_toast(res)
      },
      fail: function () {
        reject();
        util.show_fail_toast()
      },
    })
  })
}
// 课程详情正在拼的团
function get_already_group(params) {
  return new Promise((resolve,reject)=>{
    wx.request({
      url: api.data.requestUrl + api.data.getAlreadyGroup,
      header: {
        'content-type': 'application/json;charset=UTF-8'
      },
      method: 'GET',
      data: params,
      success: function (res) {
        resolve(res);
        util.show_success_toast(res)
      },
      fail: function () {
        reject();
        util.show_fail_toast()
      },
    })
  })
}
// 判断当前是否已经有拼团
function get_user_group(params,token) {
  return new Promise((resolve,reject)=>{
    wx.request({
      url: api.data.requestUrl + api.data.getUserGroup,
      header: {
        'content-type': 'application/json;charset=UTF-8',
        'x-access-token': token
      },
      method: 'GET',
      data: params,
      success: function (res) {
        resolve(res);
        util.show_success_toast(res)
      },
      fail: function () {
        reject();
        util.show_fail_toast()
      },
    })
  })
}
// 用户拼团下单
function create_goup_order(params,token) {
  return new Promise((resolve,reject)=>{
    wx.request({
      url: api.data.requestUrl + api.data.createGroupOrder,
      header: {
        'content-type': 'application/json;charset=UTF-8',
        'x-access-token': token
      },
      method: 'POST',
      data: params,
      success: function (res) {
        resolve(res);
        util.show_success_toast(res)
      },
      fail: function () {
        reject();
        util.show_fail_toast()
      },
    })
  })
}
// 单个团详情
function get_group_detail(params) {
  return new Promise((resolve,reject)=>{
    wx.request({
      url: api.data.requestUrl + api.data.getGroupDetail,
      header: {
        'content-type': 'application/json;charset=UTF-8'
      },
      method: 'GET',
      data: params,
      success: function (res) {
        resolve(res);
        // util.show_success_toast(res)
      },
      fail: function () {
        reject();
        util.show_fail_toast()
      },
    })
  })
}
// 判断是否已经拼成团
function isGroup(params) {
  return new Promise((resolve,reject)=>{
    wx.request({
      url: api.data.requestUrl + api.data.isGroup,
      header: {
        'content-type': 'application/json;charset=UTF-8'
      },
      method: 'GET',
      data: params,
      success: function (res) {
        resolve(res);
        util.show_success_toast(res)
      },
      fail: function () {
        reject();
        util.show_fail_toast()
      },
    })
  })
}
// 拼团单
function get_group_order(params,token) {
  return new Promise((resolve,reject)=>{
    wx.request({
      url: api.data.requestUrl + api.data.getGroupOrder,
      header: {
        'content-type': 'application/json;charset=UTF-8',
        'x-access-token': token
      },
      method: 'GET',
      data: params,
      success: function (res) {
        resolve(res);
        util.show_success_toast(res)
      },
      fail: function () {
        reject();
        util.show_fail_toast()
      },
    })
  })
}
// 拼团单详情
function get_order_info(params,token) {
  return new Promise((resolve,reject)=>{
    wx.request({
      url: api.data.requestUrl + api.data.getOrderInfo,
      header: {
        'content-type': 'application/json;charset=UTF-8',
        'x-access-token': token
      },
      method: 'GET',
      data: params,
      success: function (res) {
        resolve(res);
        // util.show_success_toast(res)
      },
      fail: function () {
        reject();
        util.show_fail_toast()
      },
    })
  })
}
// 拼团活动添加浏览人数
function group_browse(params) {
  return new Promise((resolve,reject)=>{
    wx.request({
      url: api.data.requestUrl + api.data.groupBrowse,
      header: {
        'content-type': 'application/json;charset=UTF-8',
        'x-access-token': wx.getStorageSync('token')?wx.getStorageSync('token'):''
      },
      method: 'POST',
      data: params,
      success: function (res) {
        resolve(res);
        // util.show_success_toast(res)
      },
      fail: function () {
        reject();
        util.show_fail_toast()
      },
    })
  })
}
// 拼团活动添加浏览人数
function group_share(params) {
  return new Promise((resolve,reject)=>{
    wx.request({
      url: api.data.requestUrl + api.data.groupShare,
      header: {
        'content-type': 'application/json;charset=UTF-8',
        'x-access-token': wx.getStorageSync('token')?wx.getStorageSync('token'):''
      },
      method: 'POST',
      data: params,
      success: function (res) {
        resolve(res);
        // util.show_success_toast(res)
      },
      fail: function () {
        reject();
        util.show_fail_toast()
      },
    })
  })
}
// 生成小程序码
function create_code(params) {
  return new Promise((resolve,reject)=>{
    wx.request({
      url: api.data.requestUrl + api.data.createCode,
      header: {
        'content-type': 'application/json;charset=UTF-8'
      },
      method: 'GET',
      data: params,
      success: function (res) {
        resolve(res);
        // util.show_success_toast(res)
      },
      fail: function () {
        reject();
        util.show_fail_toast()
      },
    })
  })
}
// 拼团活动状态
function get_group_type(params) {
  return new Promise((resolve,reject)=>{
    wx.request({
      url: api.data.requestUrl + api.data.getGroupType,
      header: {
        'content-type': 'application/json;charset=UTF-8'
      },
      method: 'GET',
      data: params,
      success: function (res) {
        resolve(res);
        // util.show_success_toast(res)
      },
      fail: function () {
        reject();
        util.show_fail_toast()
      },
    })
  })
}
// 课程套餐详情(拼团支付)
function get_group_meal_detail(params,token) {
  return new Promise((resolve,reject)=>{
    wx.request({
      url: api.data.requestUrl + api.data.getGroupMealDetail,
      header: {
        'content-type': 'application/json;charset=UTF-8',
        'x-access-token': token
      },
      method: 'GET',
      data: params,
      success: function (res) {
        resolve(res);
        util.show_success_toast(res)
      },
      fail: function () {
        reject();
        util.show_fail_toast()
      },
    })
  })
}


module.exports={
  get_group_list,
  get_cour_and_group,
  get_already_group,
  get_user_group,
  create_goup_order,
  get_group_detail,
  isGroup,
  get_group_order,
  get_order_info,
  group_browse,
  group_share,
  create_code,
  get_group_type,
  get_group_meal_detail
}